﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;

namespace PHWCFService
{
    // NOTE: If you change the class name "PHService" here, you must also update the reference to "PHService" in Web.config.
    public class PHService : IPHService
    {
        HelperFunctions hlp = new HelperFunctions();
        public IEnumerable<TimeSeriesData> getTimeSeries(string ticker, string startdate, string enddate)
        {
            XmlDocument xmlDoc = hlp.getXML(ticker, startdate, enddate);
            XDocument xDoc = XDocument.Parse(xmlDoc.InnerXml);
            IEnumerable<TimeSeriesData> ret = from item in xDoc.Descendants("TimeSeries")
                                              select new TimeSeriesData
                                              {
                                                  adjclose = Convert.ToDouble(item.Element("AdjClose").Value),
                                                  close = Convert.ToDouble(item.Element("Close").Value),
                                                  high = Convert.ToDouble(item.Element("High").Value),
                                                  low = Convert.ToDouble(item.Element("Low").Value),
                                                  open = Convert.ToDouble(item.Element("Open").Value),
                                                  volume = Convert.ToDouble(item.Element("Volume").Value),
                                                  date = Convert.ToDateTime(item.Element("Date").Value)
                                              };
            return ret;
        }
    }
}
